<template>
<div>
  <el-row>
        <el-col>
            <el-button type="danger" icon="el-icon-plus" @click="newadd">添加</el-button>
        </el-col>
    </el-row>


<el-table ref="multipleTable" :data="tb" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-loading="loading" element-loading-text="数据加载中...">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column prop="floorName" label="楼层名称">
    </el-table-column>
     <el-table-column label="店铺名称">
        <template slot-scope="scope">
            <span v-if="scope.row.shopId==0">
                全部
            </span>
            <span v-else>
                {{shopname(scope.row.shopId)}}
            </span>
        </template>
    </el-table-column>
    <el-table-column prop="position" label="楼层位置">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.position==1">微商城</el-tag>
            <el-tag v-if="scope.row.position==0">小程序</el-tag>
        </template>
    </el-table-column>
    <el-table-column prop="sort" label="排序">
    </el-table-column>
    <el-table-column prop="status" label="是否启用">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.status==0">启用</el-tag>
            <el-tag v-if="scope.row.status==1" type="info">未启用</el-tag>
        </template>
    </el-table-column>
    <el-table-column label="操作">
        <template slot-scope="scope">
        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)" v-if="delbtn(scope.row.shopId)">删除</el-button>
        <el-button size="mini" type="primary" @click="bjlcx(scope.$index, scope.row)">编辑楼层项</el-button>
        </template>
    </el-table-column>
</el-table>



<el-row>
    <el-col :span="8" :offset="16">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            layout="total, prev, pager, next, jumper"
            :total="count">
        </el-pagination>
    </el-col>
</el-row>
<!-- 分页 -->
<SetFloor :Position="position">
</SetFloor>
<FloorItemList>
</FloorItemList>
</div>
</template>
<script>
import SetFloor from '../SetWxConfig/SetFloor'
import FloorItemList from '../SetWxConfig/FloorItemList'
    export default{
        data(){
            return{
                tb:[],
                currentPage:1,
                count:0,
                loading:false,
                watchshopid:-1,
                shiros:this.$store.state.shiro,
                position:0,
                opt:[]
            }
        },
        components:{
            SetFloor,
            FloorItemList
        },
        created(){
            this.getshop()
        },
        activated(){
            var url=window.location.href;
            if(url.indexOf('wxw')!=-1){
                this.position=1
            }
            if(url.indexOf('xcxt')!=-1){
                this.position=0
            }

            var pa=new URLSearchParams()
            pa.append('position',this.position)
            if(!this.$store.state.shiro){
                if(this.$store.state.TOPshopID!=''){
                    pa.append('shopId',this.$store.state.TOPshopID)
                }else{
                    this.$message.error('数据错误')
                    return
                }
            }
            this.getData(pa)

            if(this.$store.state.TOPshopID!=''){
                this.watchshopid=this.$store.state.TOPshopID
            }
        },
        computed:{
            getstate(){
                return this.$store.state.floornew;
            },
            getshopid(){
                return this.$store.state.TOPshopID;
            },
        },
        watch:{
            getstate(va){//监听弹窗
                if(!va){
                    this.$store.state.floornew=true
                    var pa=new URLSearchParams()
                    pa.append('position',this.position)
                    if(!this.$store.state.shiro){
                        if(this.$store.state.TOPshopID!=''){
                            pa.append('shopId',this.$store.state.TOPshopID)
                        }else{
                            this.$message.error('数据错误')
                            return
                        }
                    }
                    this.getData(pa)
                }
            },
            getshopid(v){//监听右上角店铺选择
                if(v==-1){
                    var pa=new URLSearchParams()
                    pa.append('position',this.position)
                    this.watchshopid=-1
                    this.getData(pa)
                }else{
                    var param = new URLSearchParams()
                    param.append('position',this.position)
                    this.watchshopid=this.$store.state.TOPshopID
                    param.append("shopId",this.$store.state.TOPshopID)
                    this.getData(param)
                }
            },
            $route(to, from) {//监听路由,判断是小程序还是微商城,请求相关数据
                if ('/WxConfig/wxw' === to.path||'/WxConfig/xcxt' === to.path) {
                    var url=window.location.href;
                    if(url.indexOf('wxw')!=-1){
                        this.position=1
                    }
                    if(url.indexOf('xcxt')!=-1){
                        this.position=0
                    }

                    var pa=new URLSearchParams()
                    pa.append('position',this.position)
                    if(!this.$store.state.shiro){
                        if(this.$store.state.TOPshopID!=''){
                            pa.append('shopId',this.$store.state.TOPshopID)
                        }else{
                            this.$message.error('数据错误')
                            return
                        }
                    }
                    this.getData(pa)


                    if(this.$store.state.TOPshopID!=''){
                        this.watchshopid=this.$store.state.TOPshopID
                    }
                }
            }
        },
        methods:{
            shopname(v){//依据shopId获取店铺名称
                for(var i=0;i<this.opt.length;i++){
                    if(v==this.opt[i].shopId){
                        return this.opt[i].shopName
                    }
                }
            },
            getshop(){//获取店铺列表
                var _this=this
                this.opt=[]
                axios.get('/shop/listShop.do').then(r=>{
                    if(r.data.code!=200){
                        _this.$message.error(r.data.message)
                        return
                    }
                    _this.opt=[].concat(r.data.response.shopList)
                })
            },
            delbtn(shopId){
                if(this.$store.state.shiro){
                    return true
                }else{
                    if(shopId!=0){
                        return true
                    }else{
                        return false
                    }
                }
            },
            handleEdit(i,r){//修改,带参数请求弹窗
                this.$store.commit("ChangeFloor",r)
            },
            handleDelete(i,r){//删除
                var _this=this
                this.$confirm('确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var pp=new URLSearchParams()
                    pp.append("floorId",r.floorId)
                    axios.get('/deleteFloorById.do',{params:pp}).then(r=>{
                        // console.log("楼层删除",r)
                        if(r.data.code==200){
                            this.$message({
                                message:'删除成功',
                                type:'success'
                            })
                            var pa=new URLSearchParams()
                            pa.append('position',this.position)
                            if(!this.$store.state.shiro){
                                if(this.$store.state.TOPshopID!=''){
                                    pa.append('shopId',this.$store.state.TOPshopID)
                                }else{
                                    this.$message.error('数据错误')
                                    return
                                }
                            }
                            this.getData(pa)
                        }else{
                            this.$message({
                                message:r.data.message,
                                type:'info'
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            handleSizeChange(i){//翻页
                var pa=new URLSearchParams()
                pa.append('position',this.position)
                pa.append('pageNo',i)
                if(this.$store.state.TOPshopID!=''&&this.$store.state.TOPshopID!=-1){
                    pa.append('shopId',this.$store.state.TOPshopID)
                }
                this.getData(pa)
            },
            handleCurrentChange(i){//翻页
                var pa=new URLSearchParams()
                pa.append('position',this.position)
                pa.append('pageNo',i)
                if(this.$store.state.TOPshopID!=''&&this.$store.state.TOPshopID!=-1){
                    pa.append('shopId',this.$store.state.TOPshopID)
                }
                this.getData(pa)
            },
            handleSelectionChange(){
                
            },
            newadd(){//新增
                this.$store.commit("ChangeFloor")
            },
            getData(v){//获取数据
                var _this=this
                this.td=[]

                this.loading=true
                axios.get('/listFloor.do',{params:v}).then(r=>{
                    // console.log(r)
                    if(r.data.code==200){
                        // _this.tb=[].concat(r.data.response.floors)
                        var advlist=r.data.response.floors.pagedList
                        _this.tb=[]
                        if(_this.$store.state.shiro){//超管
                            // if(_this.watchshopid==-1||_this.watchshopid==''||_this.watchshopid==null){//如果选择的是全部店铺
                                _this.tb=[].concat(r.data.response.floors.pagedList)
                                // console.log("超管1",_this.tb)
                            // }else if(_this.watchshopid!=-1&&_this.$store.state.TOPshopID!=-1&&_this.$store.state.TOPshopID!=''){
                            //     for(var i = 0 ; i < advlist.length ; i++){
                            //         if(_this.watchshopid==advlist[i].shopId){
                            //             _this.tb.push(advlist[i])
                            //         }
                            //     }
                            //     // console.log("超管2",_this.tb)
                            // }
                        }else{//店长
                            _this.tb=[].concat(r.data.response.floors.pagedList)
                            // console.log("店长",_this.tb)
                        }
                        _this.count=r.data.response.floors.page1.totalCount
                    }else{
                        // _this.$message.error(r.data.message)
                        console.log(r.data.message)
                    }
                    _this.loading=false
                })
            },
            bjlcx(i,r){
                this.$store.commit('ChangeFloorGoodList',r)
            }
        }
    }
</script>